package com.haixiaoke.flat.lock.controller;

import java.util.Date;
import java.util.List;

import com.haixiaoke.auth.AuthCheck;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.lock.domain.DeviceLock;
import com.haixiaoke.saas.lock.service.IDeviceLockService;
import com.haixiaoke.saas.system.BaseController;
import com.haixiaoke.token.FrontTokenComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 设备锁Controller
 *
 * @author panda
 * @date 2025-04-02
 */
@RestController
@RequestMapping("/front/flat/lock/deviceLock")
public class FlatDeviceLockController extends BaseController {
    @Autowired
    private IDeviceLockService deviceLockService;
    @Autowired
    private FrontTokenComponent frontTokenComponent;

    /**
     * 查询设备锁列表
     */
    @AuthCheck
    @GetMapping("/page")
    public TableDataInfo page(DeviceLock deviceLock) {
        startPage();
        deviceLock.setUserId(frontTokenComponent.getUserIdNotNull());
        List<DeviceLock> list = deviceLockService.selectDeviceLockList(deviceLock);
        return getDataTable(list);
    }

    /**
     * 查询设备锁列表
     */
    @GetMapping("/list")
    public AjaxResult list(DeviceLock deviceLock) {
        deviceLock.setUserId(frontTokenComponent.getUserIdNotNull());
        List<DeviceLock> list = deviceLockService.selectDeviceLockList(deviceLock);
        return success(list);
    }

    /**
     * 获取设备锁详细信息
     */
    @GetMapping(value = "/{lockId}")
    public AjaxResult getInfo(@PathVariable("lockId") Long lockId) {
        return success(deviceLockService.selectDeviceLockById(lockId));
    }

    /**
     * 新增设备锁
     */
    @AuthCheck
    @PostMapping
    public AjaxResult add(@RequestBody DeviceLock deviceLock) {
        return deviceLockService.insertDeviceLock(deviceLock, frontTokenComponent.getLoginUser().getUser());
    }

    /**
     * 修改设备锁
     */
    @PutMapping
    public AjaxResult edit(@RequestBody DeviceLock deviceLock) {
        DeviceLock lock = new DeviceLock();
        lock.setLockId(deviceLock.getLockId());
        lock.setLockName(deviceLock.getLockName());
        lock.setUpdateBy(frontTokenComponent.getLoginUser().getUser().getUserName());
        return deviceLockService.updateDeviceLock(lock);
    }

    /**
     * 删除设备锁
     */
    @AuthCheck
	@DeleteMapping("/{lockId}")
    public AjaxResult remove(@PathVariable Long lockId) {
        return deviceLockService.deleteDeviceLockByLockId(lockId, frontTokenComponent.getUserIdNotNull());
    }

    /**
     * 开锁记录
     */
    @GetMapping("/unlockingRecord")
    @AuthCheck
    public TableDataInfo unlockingRecord(@RequestParam(value = "lockId") Long lockId,
                                             @RequestParam(value = "unlockTime", required = false) Date unlockTime,
                                             @RequestParam(value = "userName", required = false) String userName,
                                             @RequestParam(value = "pwdId", required = false) Long pwdId,
                                             @RequestParam(value = "unlockType", required = false) Integer unlockType,
                                             @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                             @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        return deviceLockService.unlockingRecord(lockId, unlockTime, userName, pwdId, unlockType, pageNum, pageSize);
    }
}
